#include "cipher.h"
#include <openssl/sha.h>
#include <QDebug>

CHash::CHash(QObject *parent) : CCipher(parent)
{

}

bool CHash::encrypt(const QByteArray &in,QByteArray &out,int mode)
{
    OpenSSL_add_all_digests();

    const EVP_MD *md = nullptr;

    if(mode == 0)
    {
        md = EVP_get_digestbyname("SHA1");
    }
    else if(mode == 1)
    {
        md = EVP_get_digestbyname("SHA224");
    }
    else if(mode == 2)
    {
        md = EVP_get_digestbyname("SHA256");
    }
    else if(mode == 3)
    {
        md = EVP_get_digestbyname("SHA384");
    }
    else if(mode == 4)
    {
        md = EVP_get_digestbyname("SHA512");
    }
    else if(mode == 5)
    {
        md = EVP_get_digestbyname("RIPEMD160");
    }
    else if(mode == 6)
    {
        md = EVP_get_digestbyname("MD5");
    }

    if(!md) // 不支持的格式
    {
        return -1;
    }

    unsigned char mdValue[EVP_MAX_MD_SIZE] = {0};
    unsigned int mdLen = 0;

    EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
    EVP_MD_CTX_init(mdctx);
    EVP_DigestInit_ex(mdctx, md, nullptr);
    EVP_DigestUpdate(mdctx, in.data(), in.size());
    EVP_DigestFinal_ex(mdctx, mdValue, &mdLen);
    EVP_MD_CTX_free(mdctx);

    QByteArray out_data((const char *)mdValue, mdLen);
    out = out_data;
}

bool CHash::encrypt(const QByteArray &in, QByteArray &out, const QByteArray &key, bool enc, int mode)
{
}

bool CHash::encrypt(const QByteArray &in, QByteArray &out, const QByteArray &key, bool enc, int mode, const QByteArray &ivec)
{
}
